package io.dataease.config;

import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import io.dataease.base.domain.*;
import io.dataease.commons.utils.CompressUtils;
import io.dataease.commons.utils.MybatisInterceptorConfig;
import io.dataease.interceptor.MybatisInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.*;

@Configuration
@MapperScan(basePackages = {"io.dataease.base.mapper", "io.dataease.plugins"}, sqlSessionFactoryRef = "sqlSessionFactory")
@EnableTransactionManagement
public class MybatisConfig {

    @Bean
    @ConditionalOnMissingBean
    public PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("pageSizeZero", "true");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }

    @Bean
    @ConditionalOnMissingBean
    public MybatisInterceptor dbInterceptor() {
        MybatisInterceptor interceptor = new MybatisInterceptor();
        List<MybatisInterceptorConfig> configList = new ArrayList<>();
        configList.add(new MybatisInterceptorConfig(FileContent.class, "file", CompressUtils.class, "zip", "unzip"));
        configList.add(new MybatisInterceptorConfig(Datasource.class, "configuration"));
        configList.add(new MybatisInterceptorConfig(AuthSource.class, "configuration"));
        interceptor.setInterceptorConfigList(configList);
        return interceptor;
    }

    @Bean
    public DataSource dataSource() {
        Map<Object, Object> map = new HashMap<>();
        HikariDataSource dataease = new HikariDataSource();
        dataease.setJdbcUrl("jdbc:mysql://172.16.16.242/hrsystem_dataease?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
        dataease.setPassword("ZvJQbPPymm71wfU7");
        dataease.setUsername("hrsystem_dataease");
        dataease.setDriverClassName("com.mysql.cj.jdbc.Driver");
        HikariDataSource client = new HikariDataSource();
        client.setJdbcUrl("jdbc:mysql://172.16.16.72:3306/hzhrsys_hdv?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
        client.setPassword("FabMBNTtHzF6YX8M");
        client.setUsername("hzhrsys_hdv");
        client.setDriverClassName("com.mysql.cj.jdbc.Driver");
        map.put("dataease",dataease);
        map.put("client",client);
        DynamicDatasource dynamicDatasource = new DynamicDatasource();
        dynamicDatasource.setTargetDataSources(map);
        dynamicDatasource.setDefaultTargetDataSource(dataease);
        return dynamicDatasource;
    }
}
